<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>Trainer后台管理系统</h2>
        <p>请登录您的账户</p>
      </div>
      
      <el-form 
        ref="loginForm" 
        :model="loginForm" 
        :rules="loginRules" 
        class="login-form"
        @submit.native.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            size="large"
            clearable
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            size="large"
            show-password
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            size="large"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminLogin',
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // 检查是否有保存的登录信息
    const savedLogin = localStorage.getItem('savedLogin');
    if (savedLogin) {
      const { username, password, rememberMe } = JSON.parse(savedLogin);
      if (rememberMe) {
        this.loginForm.username = username;
        this.loginForm.password = password;
        this.loginForm.rememberMe = true;
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          
          try {
            // 模拟登录API调用
            const response = await this.loginApi();
            
            // 保存登录信息
            if (this.loginForm.rememberMe) {
              localStorage.setItem('savedLogin', JSON.stringify({
                username: this.loginForm.username,
                password: this.loginForm.password,
                rememberMe: true
              }));
            } else {
              localStorage.removeItem('savedLogin');
            }
            
            // 保存登录状态到Vuex
            this.$store.dispatch('login', {
              token: response.data.token,
              userInfo: {
                username: this.loginForm.username,
                role: response.data.role || 'admin',
                avatar: response.data.avatar || ''
              }
            });
            
            this.$message.success('登录成功');
            
            // 跳转到首页
            this.$router.push('/');
            
          } catch (error) {
            this.$message.error(error.message || '登录失败');
          } finally {
            this.loading = false;
          }
        }
      });
    },
    
    // 登录API调用
    async loginApi() {
      // 导入API函数
      const { login } = await import('@/utils/api');
      
      // 调用真实登录API
      const response = await login({
        username: this.loginForm.username,
        password: this.loginForm.password
      });
      
      return response;
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 20px;
  }
}
</style>
